<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="在网上有很多关于这个的解决方案，我也采用了        写的比较详细的如：        　1。 改表法。可能是你的帐号不允许从远程登陆，只能在localhost。这个时候只要在localhost的那台电脑，登入mysql后，更改 &amp;quot;mysql&amp;quot; 数据库里的 &amp;quot;user&amp;quot; 表里的 &amp;quot;host&amp;quot; 项，从&amp;quot;">
<meta property="og:type" content="article">
<meta property="og:title" content="远程无法访问linux Mysql解决方案">
<meta property="og:url" content="http://yoursite.com/2017/06/25/远程无法访问linux Mysql解决方案/index.html">
<meta property="og:site_name" content="xuwujing&#39;s blog">
<meta property="og:description" content="在网上有很多关于这个的解决方案，我也采用了        写的比较详细的如：        　1。 改表法。可能是你的帐号不允许从远程登陆，只能在localhost。这个时候只要在localhost的那台电脑，登入mysql后，更改 &amp;quot;mysql&amp;quot; 数据库里的 &amp;quot;user&amp;quot; 表里的 &amp;quot;host&amp;quot; 项，从&amp;quot;">
<meta property="og:updated_time" content="2017-06-25T06:50:29.244Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="远程无法访问linux Mysql解决方案">
<meta name="twitter:description" content="在网上有很多关于这个的解决方案，我也采用了        写的比较详细的如：        　1。 改表法。可能是你的帐号不允许从远程登陆，只能在localhost。这个时候只要在localhost的那台电脑，登入mysql后，更改 &amp;quot;mysql&amp;quot; 数据库里的 &amp;quot;user&amp;quot; 表里的 &amp;quot;host&amp;quot; 项，从&amp;quot;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/06/25/远程无法访问linux Mysql解决方案/"/>





  <title>远程无法访问linux Mysql解决方案 | xuwujing's blog</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">xuwujing's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/06/25/远程无法访问linux Mysql解决方案/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="xuwujing">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xuwujing.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="xuwujing's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">远程无法访问linux Mysql解决方案</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-25T14:20:11+08:00">
                2017-06-25
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <div id="cnblogs_post_body"><br>    <p><br>        在网上有很多关于这个的解决方案，我也采用了<br><br>        写的比较详细的如：<br><br>        　1。 改表法。可能是你的帐号不允许从远程登陆，只能在localhost。这个时候只要在localhost的那台电脑，登入mysql后，更改 &quot;mysql&quot; 数据库里的 &quot;user&quot; 表里的 &quot;host&quot; 项，从&quot;localhost&quot;改称&quot;%&quot;<br><br>        CODE:mysql -u root -p123<br><br>        mysql&gt;use mysql;<br><br>        mysql&gt;update user set host = ‘%’ where user = ‘root’;<br><br>        mysql&gt;select host, user from user;<br><br>        　　2. 授权法。例如，你想kevin使用mypassword从任何主机连接到mysql服务器的话。<br><br>        CODE:GRANT ALL PRIVILEGES ON <em>.</em> TO ‘kevin’@’%’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;<br><br>        　　如果你想允许用户kevin从ip为192.168.101.234的主机连接到mysql服务器，并使用mypassword作为密码<br><br>        CODE:GRANT ALL PRIVILEGES ON <em>.</em> TO ‘kevin’@’192.168.101.234’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;<br><br>        　　3.如果按照上面这样做还不行(一般好像在windows下那样就可以了)那就修改下面这个配置文件吧(我是在linux环境下 大家可以找到自己mysql安装目录下的相应配置文件)<br><br>        CODE:vi /etc/mysql/my.cnf<br><br>        　　注释掉<br><br>        CODE:#bind-address = 127.0.0.1<br><br>        　　大功告成！！！<br><br>        <br><br>        然后我采用的是第一种方法，然后在运行完最后一句话后出了个错误ERROR 1062 (23000): Duplicate entry ‘%-root’ for key ‘PRIMARY’&nbsp;<br><br>        于是又去找解决方案，发现先<br><br>        mysql&gt; select host from user where user = ‘root’;&nbsp;<br>    </p><br>    <p><br>        <span>+———————–+&nbsp;</span><br><br>        <span>| host |&nbsp;</span><br><br>        <span>+———————–+&nbsp;</span><br><br>        <span>| % |&nbsp;</span><br><br>        <span>| 127.0.0.1 |&nbsp;</span><br><br>        <span>| localhost.localdomain |&nbsp;</span><br><br>        <span>+———————–+&nbsp;</span><br><br>        <span>3 rows in set (0.00 sec)&nbsp;</span><br><br>        <span>host已经有了%这个值，所以直接运行命令：&nbsp;</span><br>    </p><br>    <p><br>        mysql&gt;flush privileges;&nbsp;<br><br>        果然还行的通，由于对mysql不是很深入的了解，通过php果然可以远程访问了，但是我忽略了一点<br><br>        这里将host = ‘%’后，是可以远程访问了，但是我们使用root不能访问本地了<br><br>        这是由于%的意思就是允许除本机外的所有都可以访问<br><br>        <br><br>        如果前面已经如上操作了，那么你再在本地访问的时候<br><br>        #mysql -u root -p<br><br>        输入密码后会提示<br>    </p><br>    <p><br>        &nbsp;<br>    </p><br>    <p><br>        &nbsp;ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)&nbsp;<br><br>        这就很郁闷，本机已经无法登陆了，这时我们需要安全模式进入(个人理解为安全模式)<br><br>        首先，将mysql服务停止<br><br>        #/etc/init.d/mysql stop &nbsp; 或者 &nbsp;#sudo service mysqld stop<br><br>        然后再执行<br><br>        # mysqld_safe –user=mysql –skip-grant-tables –skip-networking &amp;<br><br>        记住 &nbsp;后面那个 &nbsp;&amp;是需要的<br><br>        等出现一些提示后 &nbsp;应该是在starting…之后<br><br>        直接输入 &nbsp;mysql 即可进入数据库了 &nbsp;(这里其实也可以是忘记密码后进行修改密码的方法)<br><br>        进入后，修改密码的语句为：<br><br>        mysql&gt; use mysql; &nbsp;&nbsp;<br><br>        进入数据库修改密码<br><br>        mysql&gt; update user set password = PASSWORD(‘你的新密码’) where user=’root’<br><br>        如果是要解决上面修改了host的方法就是执行<br><br>        mysql&gt; update user set host=’localhost’&nbsp;where user=’root’ and host=’%’<br><br>        将host改回来<br><br>        <br><br>        但是改回来岂不是又无法远程访问了？<br><br>        其实我们可以新增一个用户，把必要的权限授予于这个用户，且将这个用户的host设为%<br><br>        <br><br><br>    </p><br>    <p><br>        过程中出现的错误要仔细查看，要注意防火墙的状态，可能影响数据库的远端连接。<br>    </p><br></div>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/06/25/微软云linux服务器FTP文件传输错误解决办法/" rel="next" title="微软云linux服务器FTP文件传输错误解决办法">
                <i class="fa fa-chevron-left"></i> 微软云linux服务器FTP文件传输错误解决办法
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/06/25/微软云Linux服务器 Mysql、tomcat远程连接错误解决办法/" rel="prev" title="微软云Linux服务器 Mysql、tomcat远程连接错误解决办法">
                微软云Linux服务器 Mysql、tomcat远程连接错误解决办法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/xuwujing.png"
               alt="xuwujing" />
          <p class="site-author-name" itemprop="name">xuwujing</p>
           
              <p class="site-description motion-element" itemprop="description">The way of the future!</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">2</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/xuwujing" target="_blank" title="github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  github
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://blog.csdn.net/qazwsxpcm?viewmode=list" target="_blank" title="csdn">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  csdn
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xuwujing</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
